<?php
/**
 * 解析每日库存-数据入库
 *（Logic）
 */
namespace App\Logic\Amazon\Api\Parse;

use App\Models\Amazon\FBAMYIInventory;
use App\Models\Basics\SysICountry;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductSku;

class GetFbaMyiUnsuppressedInventoryData
{
    /**
     * @param $lines
     * @param $accountId
     * @param $countryId
     * @return int
     */
    public static function exe($lines,$accountId,$countryId){
        if(!is_array($lines))return 2;
        if(!$countryId)return 2;
        FBAMYIInventory::query()->where('account_id',$accountId)->where('country_id',$countryId)->delete();
        $i = 0;
        $count = 0;
        try{
            foreach($lines as $line){
                $flag = self::_addFbaMYIInventory($line,$accountId,$countryId);
                $flag && $count++;
                $i++;
            }
        }catch (\Exception $e){
            report($e);
        }
        return $i == $count ? 1 : 2;
    }

    /**
     * @param $line
     * @param $accountId
     * @param $countryId
     * @return bool
     */
    private static function _addFbaMYIInventory($line,$accountId,$countryId){
        $count = FBAMYIInventory::query()
            ->where('country_id',$countryId)
            ->where('sku',$line['sku'])
            ->count();
        if(array_key_exists('afn-fulfillable-quantity',$line)){
            $afnFulfillableQuantity = $line['afn-fulfillable-quantity'];
        }else{
            $afnFulfillableQuantity = $line['afn-fulfillable-quantity-local'] + $line['afn-fulfillable-quantity-remote'];
        }

        $pskuId = ProductAmazonSku::getPskuIdBySellerSku($line['sku']); //skuID
        if ($pskuId == 0) {
            $pskuCode = substr($line['sku'], (stripos($line['sku'], 'AM0')),7); //特殊处理,非正常匹配ERP产品
            if ($pskuCode) {
                $pskuId = ProductSku::getValueByCode($pskuCode,'id'); //skuID
            }
        }
        $areaId = SysICountry::getAreaIdByCountryId($countryId); //areaID

        if($count > 0){
            $flag = FBAMYIInventory::query()
                ->where('country_id',$countryId)
                ->where('sku',$line['sku'])
                ->update(
                    [
                        'account_id' => $accountId,
                        'area_id' => $areaId,
                        'psku_id' => $pskuId,
                        'fnsku' => $line['fnsku'],
                        'asin' => $line['asin'],
                        'afn_fulfillable_quantity' => $afnFulfillableQuantity,
                        'afn_reserved_quantity' => $line['afn-reserved-quantity'],
                        'afn_researching_quantity' => $line['afn-researching-quantity'],
                        'afn_inbound_working_quantity' => $line['afn-inbound-working-quantity'],
                        'afn_inbound_shipped_quantity' => $line['afn-inbound-shipped-quantity'],
                        'afn_inbound_receiving_quantity' => $line['afn-inbound-receiving-quantity'],
                    ]
                );
            return !$flag ? false : true;
        }else{
            $re = FBAMYIInventory::query()->create(
                [
                    'account_id' => $accountId,
                    'country_id' => $countryId,
                    'area_id' => $areaId,
                    'psku_id' => $pskuId,
                    'sku' => $line['sku'],
                    'fnsku' => $line['fnsku'],
                    'asin' => $line['asin'],
                    'afn_fulfillable_quantity' => $afnFulfillableQuantity,
                    'afn_reserved_quantity' => $line['afn-reserved-quantity'],
                    'afn_researching_quantity' => $line['afn-researching-quantity'],
                    'afn_inbound_working_quantity' => $line['afn-inbound-working-quantity'],
                    'afn_inbound_shipped_quantity' => $line['afn-inbound-shipped-quantity'],
                    'afn_inbound_receiving_quantity' => $line['afn-inbound-receiving-quantity'],
                ]
            );
            return !$re ? false : true;
        }
    }
}
